<template>
	<view class="container">
		<view class='login_content'>
			<view class='user_photo'>
				<!-- <open-data type="userAvatarUrl"></open-data> -->
			</view>
			<view class='user_nickname'>
				<!-- <open-data type="userNickName"></open-data> -->
			</view>
			<button  v-if="canIUse" class="login_btn" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">点击授权登录</button>
			<!-- <button v-if="canIUse" class="login_btn" @click="wxLogin">点击授权登录</button> -->
			<view v-else>请升级微信版本</view>
			<button class="back_btn" @click="toHome">返回首页</button>
		</view>
	</view>
</template>
<script>
	import {
		mapState,
		mapGetters,
		mapMutations,
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				canIUse: uni.canIUse('button.open-type.getUserInfo'),
				loginType: ''
			}
		},
		methods: {
			...mapActions('userInfo', [
				'setUserInfoAsync' // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
			]),
			wxLogin(){
				
			},
			async bindGetUserInfo(e) {
				var _this = this;
				uni.login({
					success: res => {
						let code = res.code;
						uni.getUserInfo({
							success: res => {
								_this.setUserInfoAsync(res); //把获取到的用户的信息存放在vuex中进行管理
								_this.$api.postCode({
									olduser_id: uni.getStorageSync('olduser_id') || '',
									raw_data: res.rawData,
									signature: res.signature,
									code: code
								}).then(res1 => {
									// _this.$request.postData('/auth/wxLogin',{raw_data:res.rawData,signature:res.signature,code:code}).then(res1 =>{
									if (res1.data.level == "success") {
										uni.setStorageSync('userToken', res1.data.data);
										_this.getUserInfo()
										if (res1.data.action == 'home') {
											uni.showLoading({
												title: '加载中',
											})
											uni.switchTab({
												url: "/pages/home/index"
											})
										} else {
											uni.navigateTo({
												url: '/pages/register/index'
											})
										}
									} else {
										console.log(res1.data.message)
										uni.showToast({
											mask: true,
											icon: 'none',
											title: res1.data.message,

										})
									}
								})
							}

						})
					}
				})


			},
			toHome() {
				uni.switchTab({
					url: "/pages/home/index"
				})
			},
			async getUserInfo() {
				let userToken = uni.getStorageSync('userToken');
				if (userToken) {
					let res = await this.$api.getUserInfo({
						user_token: userToken
					})
					if (res.data.level == 'success') {
						this.setUserInfoAsync(res.data.data)
					}
				}

			}
		},
		onLoad(options) {

		},

	}
</script>
<style scoped>
	.login_content {
		width: 100%;
		position: absolute;
		left: 0;
		top: 20%;
	}

	.user_photo {
		width: 200px;
		margin: 0 auto;
	}

	.user_nickname {
		text-align: center;
		margin-top: 20px
	}

	button {
		margin: 30px auto 10px auto;
		border-radius: 2px;
		width: 50%;
		border: solid 1px green;
		background-color: white;
		color: green
	}

	.back_btn {
		width: 50%;
		text-align: center;
		margin: 10px auto;
		border: solid 1px green;
		background-color: white;
		color: green
	}
</style>
